package managers.messengerManager;

import java.awt.Color;
import java.util.HashMap;

import javax.swing.JOptionPane;

import spotGUI.mainFrameGUI.MainFrame;
import spotGUI.trayIcon.DefaultTrayIconManager;
import spotGUI.trayIcon.TrayIconManager;
import utils.UserData;
import utils.UserState;

public class SpotFeedbackManager implements FeedbackManager {

	private MainFrame feedBackView;
	private TrayIconManager trayIconManager;
	
	private HashMap<UserState, String> iconsMap;
	
	public SpotFeedbackManager (MainFrame feedBackView) {
		
		this.feedBackView = feedBackView;
		
		iconsMap = new HashMap<UserState, String>();
		fillIconsMap();
		
		trayIconManager = new DefaultTrayIconManager(this);
		
	}
	


	
	
	
	public void updateStatus (String message, int type) {
		switch (type) {
		case WARNING:
			feedBackView.getStatusBar().setStatus(message, Color.RED.darker().darker());
			break;
		default:
			feedBackView.getStatusBar().setStatus(message, Color.BLACK);
		}
		
	}
	
	public void addUserView (UserData user) {
		feedBackView.getMessengerPanel().fillSingleListCell(user);
	}

	public void update() {
		feedBackView.getMessengerPanel().repaint();
	}
	
	public void feedbackNotification () {
		feedBackView.setVisible(true);
	}
	
	public void printErrorAndExit (String message) {
		JOptionPane.showMessageDialog(feedBackView, message, "Errore", JOptionPane.ERROR_MESSAGE);
		System.exit(-1);
	}
	
	public void changeState (UserState state) {
		feedBackView.getPersonalStateBar().programmaticSetState(state);
		trayIconManager.updateTrayIconState(state);
	}
	
	private void fillIconsMap () {
		iconsMap.put(UserState.AVAILABLE, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/accept.png").getPath());
		iconsMap.put(UserState.BUSY, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.ON_THE_PHONE, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.EATING, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.SLEEPING, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.BE_RIGHT_BACK, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.STUDYING, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.LISTENING_TO_MUSIC, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
		iconsMap.put(UserState.PLAYING, SpotFeedbackManager.class.getResource("/images/Icons/24x24px/acceptYellow.png").getPath());
	}



	public void showTrayIcon(boolean show) {
		if (show)
			trayIconManager.showTrayIcon();
		else
			trayIconManager.hideTrayIcon();
	}

	public String getIconPath (UserState state) {
		return iconsMap.get(state);
	}


	@Override
	public void setPersonalMessage(String message) {
		feedBackView.getPersonalStateBar().setPersonalMessage(message);
		
	}

	@Override
	public void enableUserInput(boolean enable) {
		feedBackView.setEnabled(enable);
		
	}
}
